﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>双益ERP系统登录首页</title>
    <link href="~/Scripts/jquery-easyui/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />

    <script type="text/javascript">
        if (window.parent.window != window) {
            window.top.location.href = "/Login/Index";
        }

        $(function () {
            $("#loginDialog").dialog({
                title: "用户登录",
                closable: false,
                iconCls: 'icon-user_b',
                modal: true,
                width: 310,
                height: 220,
                buttons: [{
                    id: "loginBtn",
                    text: "登 录",
                    handler: function () {
                        if ($("#loginFrm").form('validate')) {
                            login();
                        }
                    }
                }]
            });

            //回车提交表单
            $("#loginFrm").find('input').on('keyup', function (event) {
                if (event.keyCode == '13') {
                    login();
                }
            })

            //创建验证码
            $("#valiCode").bind("click", function () {
                this.src = "@Url.Action("GetValidatorGraphics", "Validator")?time=" + (new Date()).getTime();
            });

            //20190409，ben
           // var cookie_userName = getCookieValue('cookie_userName');
            var cookie_userName = getCookie('cookie_userName');
            $("#loginName").val(cookie_userName);
      
            //本地运行时使用 发布时注释,测试默认admin登陆
            //var code = getCookie('ValidatorCode');
            //$("#loginVerificationCode").val(code);
            //$("#loginName").val('admin');
            //$("#loginPwd").val('123456');
            //$("#loginName").focus();
        })

        function login() {
            if (checkInput()) {
                var postData = {
                    AccountName: $("#loginName").val(),
                    Password: $("#loginPwd").val(),
                    CookieExpires: $("#remember").val()
                };
                //异步实现登录功能
                $.post("/Login/CheckUserLogin", postData, function (data) {                   
                    if (data == "OK") {
                        //addCookie("cookie_userName", $("#loginName").val(), 30, "/");
                        SetCookie("cookie_userName", $("#loginName").val());
                        window.location.href = "/Home/Index";
                    }
                    else {
                        alert(data);
                        window.location.href = "/Login/Index/";
                    }
                });
            }
        }

        //验证码验证
        function checkInput() {
            //通过cookie获取验证码
            var validatorcode = getCookie('ValidatorCode').toUpperCase();
            if ($.trim($("#loginVerificationCode").val()) == "") {
                $.show_alert("信息", "验证码不能为空");
                $("#loginVerificationCode").val("").focus();
                return false;
            }
            else if (document.getElementById("loginVerificationCode").value.toUpperCase() != validatorcode) {
                $.show_alert("信息", "验证码错误");
                $("#loginVerificationCode").val("").focus();
                $("#valiCode").attr('src', '/Validator/GetValidatorGraphics?time=' + (new Date()).getTime());
                return false;
            }
            else {
                return true;
            }
        }//20190309

        //获取cookie
        function getCookie(cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
            }
            return "";
        }

        ///20190322添加cookie处理；
        ///

        function addCookie(name, value, days, path) {
            //添加设置cooki
            var name = escape(name);
            var value = escape(value);
            var expires = new Date();
            expires.setTime(expires.getTime() + days * 3600000 * 24);
            //path=/，表示cookie能在整个网站下使用，path=/temp，表示cookie只能在temp目录下使用  
            path = path == "" ? "" : ";path=" + path;
            //GMT(Greenwich Mean Time)是格林尼治平时，现在的标准时间，协调世界时是UTC  
            //参数days只能是数字型  
            var _expires = (typeof days) == "string" ? "" : ";expires="  + expires.toUTCString();
            document.cookie = name + "=" + value + _expires + path;
        }

        //写入cookie,兼容中文
        function SetCookie(name, value) {
            var exp = new Date();
            exp.setTime(exp.getTime() + 6 * 24 * 60 * 60 * 1000); //6天过期
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
            return true;
        };
        //读取cookie，兼容中文
        function getCookie(name) {
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (arr != null) return unescape(arr[2]); return null;
        };


        //获取cookie的值，根据cookie的键获取值
        function getCookieValue(name) { //此处name是 userName,userPass
            //用处理字符串的方式查找到key对应value  
            var name = escape(name);
            //读cookie属性，这将返回文档的所有cookie  
            var allcookies = document.cookie;
            //查找名为name的cookie的开始位置  
            name += "=";
            var pos = allcookies.indexOf(name);
            //如果找到了具有该名字的cookie，那么提取并使用它的值  
            if (pos != -1) { //如果pos值为-1则说明搜索"version="失败  
                var start = pos + name.length; //cookie值开始的位置  
                var end = allcookies.indexOf(";", start); //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置 
                if (end == -1)
                    end = allcookies.length; //如果end值为-1说明cookie列表里只有一个cookie  
                var value = allcookies.substring(start, end); //提取cookie的值  
                return (value); //对它解码        
            } else { //搜索失败，返回空字符串  
                return "";
            }
        }

        function deleteCookie(name, path) {
            //根据cookie的键，删除cookie，其实就是设置其失效
            var name = escape(name);
            var expires = 0;
            alert(expires)
            path = path == "" ? "" : ";path=" + path;
            document.cookie = name + "=" + ";expires="
            + expires.toUTCString() + path;
        }

        function userLogin() {
            //用户登录，其中需要判断是否选择记住密码
            //简单验证一下  
            var userName = document.getElementById("name").value;
            if (userName == '') {
                alert("请输入用户名。");
                return;
            }

            var userPass = document.getElementById("pwd").value;
            if (userPass == '') {
                alert("请输入密码。");
                return;
            }

            var objChk = document.getElementById("checkpwd");

            if (objChk.checked) {
                //添加cookie  
                addCookie("userName", userName, 30, "/");
                addCookie("userPass", userPass, 30, "/");
                //alert("记住了你的密码登录。");
                getCookieValue();
                // window.location.href = "http://www.baidu.com";  
            } else {
                //alert("不记密码登录。");
                addCookie("userName", userName, 0, "/");
                addCookie("userPass", userPass, 0, "/");
                deleteCookie();
                // window.location.href = "http://www.baidu.com"; 
       }
        }

        //实现功能，保存用户的登录信息到cookie中。当登录页面被打开时，就查询cookie

        //window.onload = function () {
        //    var userNameValue = getCookieValue("userName");
        //    //document.getElementById("name").value = userNameValue; //取出cookies的name赋给文本框
        //    $("#name").textbox('setValue', userNameValue);
        //    var userPassValue = getCookieValue("userPass");
        ////    //document.getElementById("pwd").value = userPassValue;
        //    $("#pwd").textbox('setValue', userPassValue);
        //    //alert(" "+document.getElementById("name").value+" "+userPassValue);
        //}
 

    </script>
</head>
<body>
    <div id="loginDialog" style="padding: 15px; text-align: center">
        <form id="loginFrm" method="post">
            <table class="tableForm" style="width: 100%">
                <tr>
                    <th>
                        用户名：
                    </th>
                    <td>
                        <input type="text" id="loginName" name="loginName" class="easyui-validatebox textbox" style="width:170px;height:22px;" data-options="prompt:'UserName',required:true,validType:'length[2,20]'" />
                    </td>
                </tr>
                <tr>
                    <th>
                        密&nbsp;码：
                    </th>
                    <td>
                        <input type="password" id="loginPwd" name="loginPwd" class="easyui-validatebox textbox" style="width: 170px; height: 22px;" data-options="prompt:'Password',required:true" />
                    </td>
                </tr>
                <tr>
                    <th>
                        验证码：
                    </th>
                    <td>
                        <input type="text" class="easyui-validatebox textbox" name="loginVerificationCode" id="loginVerificationCode" style="width: 100px; height: 22px; vertical-align: middle;" />
                        <img id="valiCode" style="vertical-align: middle;" src="@Url.Action("GetValidatorGraphics", "Validator")" title="看不清？点击更换图片。" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>
